//
//  CircleButton.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/5/18.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "CircleButton.h"
#import "Masonry.h"
#import "SecretAlbumsConst.h"

#define CircleLineWidth Adapter_X(2.0f)
#define TitleFontSize Adapter_X(36.0f)

@interface CircleButton ()

@property (nonatomic, strong)CAShapeLayer *circleLayer;

@end

@implementation CircleButton

- (instancetype)initWithType:(CircleButtonType)type
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        
        self.type = type;
        
        // 添加边框
        self.circleLayer = [CAShapeLayer layer];
        self.circleLayer.lineWidth = CircleLineWidth;
        self.circleLayer.fillColor = [[UIColor clearColor] CGColor];
        [self.layer addSublayer:self.circleLayer];
        
        if (CircleButtonCancel == type) {
            // 添加cancelLayer
            self.cancelLayer = [CAShapeLayer layer];
            self.cancelLayer.lineWidth = CircleLineWidth;
            self.cancelLayer.fillColor = [[UIColor clearColor] CGColor];
            self.cancelLayer.strokeColor = [[UIColor whiteColor] CGColor];
            [self.layer addSublayer:self.cancelLayer];
        } else {
            // 添加文本
            self.titleLabel = [UILabel new];
            self.titleLabel.textColor = [UIColor whiteColor];
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
            self.titleLabel.font = [UIFont systemFontOfSize:TitleFontSize];
            [self addSubview:self.titleLabel];
        }
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 边框
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))
                                                        radius:CGRectGetWidth(self.bounds) / 2 - CircleLineWidth
                                                    startAngle:0
                                                      endAngle:M_PI * 2
                                                     clockwise:YES];
    self.circleLayer.path = path.CGPath;
    self.circleLayer.strokeColor = self.tintColor.CGColor;
    
    if (CircleButtonCancel == self.type) {
        // 取消图标
        UIBezierPath *cancelPath = [UIBezierPath bezierPath];
        
        cancelPath.lineCapStyle = kCGLineCapRound;
        cancelPath.lineJoinStyle = kCGLineCapRound;
        
        [cancelPath moveToPoint:CGPointMake(CGRectGetMidX(self.bounds) - Adapter_X(20), CGRectGetMidY(self.bounds))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) - Adapter_X(2), CGRectGetMidY(self.bounds) - Adapter_X(14))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) - Adapter_X(2), CGRectGetMidY(self.bounds) - Adapter_X(7))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) + Adapter_X(20), CGRectGetMidY(self.bounds) - Adapter_X(7))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) + Adapter_X(20), CGRectGetMidY(self.bounds) + Adapter_X(7))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) - Adapter_X(2), CGRectGetMidY(self.bounds) + Adapter_X(7))];
        [cancelPath addLineToPoint:CGPointMake(CGRectGetMidX(self.bounds) - Adapter_X(2), CGRectGetMidY(self.bounds) + Adapter_X(14))];
        [cancelPath closePath];
        
        self.cancelLayer.path = cancelPath.CGPath;
    } else {
        // 文本
        self.titleLabel.bounds = self.bounds;
        self.titleLabel.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    }
}

#pragma mark - 点击方法

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super beginTrackingWithTouch:touch withEvent:event];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(touchCircleButton:)]) {
        [self.delegate touchCircleButton:self];
    }
    
    [CATransaction begin];
    [CATransaction setAnimationDuration:0.1f];
    
    self.circleLayer.fillColor = self.tintColor.CGColor;
    
    [CATransaction commit];
    
    return YES;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super endTrackingWithTouch:touch withEvent:event];
    
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0f];
    
    self.circleLayer.fillColor = [[UIColor clearColor] CGColor];
    
    [CATransaction commit];
}

- (void)cancelTrackingWithEvent:(UIEvent *)event
{
    [super cancelTrackingWithEvent:event];
    
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0f];
    
    self.circleLayer.fillColor = [[UIColor clearColor] CGColor];
    
    [CATransaction commit];
}

@end
